package com.cskaoyan.aspect;

import com.cskaoyan.model.MarketLog;
import com.cskaoyan.service.LogService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

/**
 * @Description
 * @Author zoe ling
 * @Date 2024/6/8
 */

public class LogAspect {
    @Autowired
    LogService logService;
    //定义切入点
    @Pointcut("@annotation(com.cskaoyan.anno.LogRecord)")
    public void logPointCut(){}

    //定义通知，执行log时向数据库中添加一条记录
    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        String methodName = joinPoint.getSignature().getName();
        MarketLog marketLog = MarketLog.builder()
                .deleted(false)
                .addTime(new Date())
                .updateTime(new Date())
                .action(methodName)
                .build();
        Object proceed=null;
        try {
            proceed = joinPoint.proceed();
            if (proceed==null){
                marketLog.setStatus(false);
                marketLog.setResult("失败");
            }else {
                marketLog.setAdmin(proceed.toString());
                marketLog.setStatus(true);
                marketLog.setResult("成功");
            }
        } catch (Throwable e) {
            marketLog.setStatus(false);
            marketLog.setResult("失败");
            throw new RuntimeException(e);
        }finally {
            logService.create(marketLog);
        }
        return proceed;
    }



}
